﻿using System;
using Tensop.Net.Tcp.Protocol;

namespace Tensop.Net.Tcp
{
    public class HeaderProtocol:IProtocolBase
    {
        internal byte[] _baseData;
        public byte Protocol { get => _baseData[ 2 ]; }

        public byte Version { get => _baseData[ 3 ];}

        
        int _length;
        public int ContentLength {
            get =>_length;
            set {
                _length = value;
                Array.Copy( BitConverter.GetBytes( value ), 0, _baseData, 4, 4 );
            }
        }

        public HeaderProtocol(HeaderProtocolType type)
        {
            _baseData = new byte[ 20 ]
            {
                0x12,0x13,
                0,0,
                0,0,0,0,
                0,0,0,0,0,0,0,0,
                0x14,0x15,0x16,0x17
            };
            _baseData[3] = (byte) type;
        }

        public byte[] ToArray()
        {
            var data = BitConverter.GetBytes( DateTime.Now.Ticks );
            Array.Copy( data, 0, _baseData, 8, 8 );
            return _baseData;         
        }
    }
}
